<template>
  <scroll-view>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <!-- 移动端左滑出现删除效果  必须先uni-swipe-action  再uni-swipe-action-item > 自己的结构 -->
      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="item in patientList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="clickDel(item.id!)"
        >
          <!-- 如果想控制一个容器的类名 有或无 
            :class="item.defaultFlag === 1 ? 'active' : ''" 
          -->
          <view
            class="archive-card"
            :class="{ active: patientId === item.id }"
            @click="selectPatient(item.id!)"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')
              }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length < 6" class="archive-card">
        <!-- 添加不需要id -->
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/index">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-view>
</template>

<script lang="ts" setup>
  import { deletePatientApi, patientListApi } from '@/apis/patient'
  import type { PatientItem } from '@/types/patient'
  import { onLoad, onShow } from '@dcloudio/uni-app'
  import { ref } from 'vue'
  import { useConsultStore } from '@/stores/consult'

  // 获取路径参数
  const props = defineProps<{ type?: string }>() // 这个页面两种姿势进入   有type    没有type
  if (props.type) {
    console.log('这次需要选中患者功能')
  } else {
    console.log('这次就正常功能')
  }

  const store = useConsultStore()
  const selectPatient = (id: string) => {
    if (!props.type) {
      // 如果这次是正常进入页面  直接return
      return
    }
    patientId.value = id // 高亮
    // 存仓库
    store.setPatientId(id)
    // 直接跳转
    uni.navigateTo({
      url: '/subpkg_consult/payment/index',
    })
  }

  const patientId = ref('') //

  const patientList = ref<PatientItem[]>([])

  const getList = async () => {
    const res = await patientListApi()
    console.log(res)
    patientList.value = res
  }

  // 每次展示页面都重新获取最新数据
  onShow(() => {
    getList()
  })
  // 执行一次
  // onLoad(() => {
  //   getList()
  // })

  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])

  // 点击删除
  const clickDel = async (id: string) => {
    //点击选项按钮时触发事
    // console.log('111')

    // 删除业务:
    // 删除前确认
    const res = await uni.showModal({
      title: '警告',
      content: '你确定删除么',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
    })
    // console.log(res)
    if (res.confirm) {
      // 请求接口删除数据    封装请求函数  封装类型
      await deletePatientApi(id)

      // 获取最新数据
      getList()
      // 提示
      uni.showToast({
        title: '删除成功',
        icon: 'none',
      })

      if (store.patiendId === id) store.patiendId = ''
    }
  }
</script>

<style lang="scss" scoped>
  @import './index.scss';
</style>
